package magneticCave;

//Castiel was here

import java.util.*;
import java.io.*;

public class Leaf { 
	
	public static final int BLACK = 1;
	public static final int WHITE = 2;
	public static final int NO_ONE = 3;
	
	protected static final int ROWS = 8;
	protected static final int COLS = 8;
	
	
	private LinkedList<Leaf> nodes;
	private Leaf parent; //to parent.
	//current player whose turn it is at this point. All of the children of this node will be considered
	//the possible moves that this player can make.
	private int curPlayer; 
	private Leaf bestChild = null;
	
	private int val;
	public int[] grid;   //game grid
	private int _moveCol=0;
	private int _moveRow=0;
	
	private boolean hasChildren;
	protected static final int UNDEFINED_MOVE=0;
	
	
	public Leaf GetParent()
	{
		return parent;
	}
	
	public Leaf(int player)
	{
		curPlayer = player;
		parent = null;
		hasChildren = false;
		val = 0;
		grid = new int[ROWS * COLS];
		nodes = new LinkedList<Leaf>();
	//	val = nval;
		for(int i =0; i < grid.length; i++) //copy the grid
			grid[i] = 0;
	}

	
	public Leaf(int[] newgrid, int player, int moveRow, int moveCol)
	{
		curPlayer = player;
		parent = null;
		hasChildren = false;
		val = 0;
	//	grid = new int[ROWS * COLS];
		nodes = new LinkedList<Leaf>();
		grid = newgrid;
		
		_moveRow = moveRow;
		_moveCol = moveCol;
	//	for(int i =0; i < newgrid.length; i++) //copy the grid
		//	grid[i] = newgrid[i];
	//	val = nval;
	}
	public void AddNode(Leaf node)
	{
		node.parent = this;
		nodes.add(node);
		hasChildren = true;
	}
	
	public void SetBestChild(Leaf node)
	{
		bestChild = node;
	}
	public Leaf GetBestChild()
	{
		return bestChild;
	}
	
	public int GetBestMoveRow()
	{
		Leaf best = GetBestChild();
		if(best == null)
			return GetMoveRow();
		else
			return best.GetMoveRow();
	}
	public int GetBestMoveCol()
	{
		Leaf best = GetBestChild();
		if(best == null)
			return GetMoveCol();
		else
			return best.GetMoveCol();
	}
	
	public int GetMoveRow()
	{
		return _moveRow;
	}
	public int GetMoveCol()
	{
		return _moveCol;
	}
	public LinkedList<Leaf> GetNodes()
	{
		return nodes;
	}
	
	public int getPlayer()
	{
		return curPlayer;
	}
	
	public int GetValue()
	{
		return val;
	}
	
	public void SetValue(int newVal)
	{
		val = newVal;
	}
	
	public boolean HasChildren()
	{
		return nodes.size() >= 1;
	}
	
	
	//Print out all the values of the tree.
	public void Report(BufferedWriter writer) //startingMove from BLACK
	{
		RecReport(this, 1, writer);
	}
	
	//Print out the values of a whole tree
	private static void RecReport(Leaf leaf, int lvl, BufferedWriter writer)
	{
		//int nextMove = 0;
		
//		if(leaf.GetPlayer() == BLACK)
//			nextMove = WHITE;
//		else
//			nextMove = BLACK;

		for(Leaf o : leaf.nodes)
		{
			RecReport(o, lvl + 1, writer);
		}
		
		//make a new line for parent nodes
		if(leaf.nodes.size() > 0)
		{
			System.out.println();
			try{
			writer.write("\n");
			}catch(IOException e)
			{
				System.out.print("lvl: " + lvl + ": ");
				e.printStackTrace();
			}
		}
		String strMove;
		if(leaf.getPlayer() == BLACK)
			strMove = "Black";
		else
			strMove = "White";
		
		
		String msg = "<Level: " + lvl + ", Player: \"" + strMove + "\", Value: " + leaf.val + ">, ";
		
		System.out.print(msg);
		try{
		writer.write(msg);
		}catch(IOException e)
		{
			System.out.print("lvl: " + lvl + ": ");
			e.printStackTrace();
		}
	
	}
	
}
